<?php

/**
/**
 * Rappresenta genericamenet una query
 * @package	Query
 * @author	Domenico Biancardi <domenico.biancardi@gmail.com>
 * @link	http://code.google.com/p/syrius/
 *
 */
namespace Syrius\Query;


abstract class Query implements \Iterator{

	protected $_orderBy;

	protected $_whereConditions;

    protected $_model;

    protected $_limit;

    protected $entityList;
	/**
	 * Ritorna la versione stringa dell'oggetto che � stato creato
	 */
	public function __toString() {
	}

    public function current() {
        return $this -> entityList -> current();
	}

    /**
     * (PHP 5 &gt;= 5.1.0)<br/>
     * Move forward to next element
     * @link http://php.net/manual/en/iterator.next.php
     * @return void Any returned value is ignored.
     */
    public function next() {
        return $this -> entityList -> next();
    }

    /**
     * (PHP 5 &gt;= 5.1.0)<br/>
     * Return the key of the current element
     * @link http://php.net/manual/en/iterator.key.php
     * @return scalar scalar on success, integer
     * 0 on failure.
     */
    public function key() {
        return $this -> entityList -> key();
    }

    /**
     * (PHP 5 &gt;= 5.1.0)<br/>
     * Checks if current position is valid
     * @link http://php.net/manual/en/iterator.valid.php
     * @return boolean The return value will be casted to boolean and then evaluated.
     * Returns true on success or false on failure.
     */
    public function valid() {
        return $this -> entityList -> valid();
    }

    /**
     * (PHP 5 &gt;= 5.1.0)<br/>
     * Rewind the Iterator to the first element
     * @link http://php.net/manual/en/iterator.rewind.php
     * @return void Any returned value is ignored.
     */
    public function rewind() {
        $data = new \Syrius\Adapter\Query ($this -> __toString());
		$result = new \Syrius\Adapter\Result ($data);
		$this -> entityList = new \Syrius\Model\Entity_List ($result, $this -> _model);
    }

    public function count(){
        if ($this -> entityList == null){
            $this -> rewind();
        }
        return $this -> entityList -> count();
    }


    public function where($condition) {
		$this->_whereConditions [] = $condition;
		return $this;
	}

	public function orderBy($condition) {
		$this->_orderBy [] = $condition;
        return $this;
	}

    public function limit($start, $length){
        $this -> _limit = array("start" => $start, "length" => $length);
        return $this;
    }

}

?>
